package net.andvari.android.mobilesignalcondition;

import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.util.Log;

public final class SignalListener extends PhoneStateListener
{
	private int state = -1;
	private Context context;

	private static final Intent REQUEST_REQUERY = new Intent(com.twofortyfouram.Intent.ACTION_REQUEST_QUERY);

	static
	{
		/*
		 * The Activity name must be present as an extra in this Intent, so that Locale will know who needs updating. This intent
		 * will be ignored unless the extra is present.
		 */
		REQUEST_REQUERY.putExtra(com.twofortyfouram.Intent.EXTRA_ACTIVITY, EditActivity.class.getName());
	}
	
	public void setContext(Context c)
	{
		Log.d("GSMSignalCondition", "Setting Context");	
		context = c;	
	}
	
	public void onServiceStateChanged(ServiceState ss) 
	{
		state = ss.getState();
		Log.d("GSMSignalCondition", "Service State changed to " + ss.toString());
		context.sendBroadcast(REQUEST_REQUERY);
	}
	
	public void onSignalStrengthsChanged(SignalStrength ss)
	{
		Log.d("GSMSignalCondition", "Signal strength changed to " + ss.getGsmSignalStrength());
		context.sendBroadcast(REQUEST_REQUERY);
	}
	
	public int getState()
	{
		return state;
	}
}